package ldh.im.fxbase.util;

import javafx.scene.shape.*;

public class PathElementUtil {

    private static final double h = 2.0d;

    public static PathElement[] bigHeight(PathElement[] pes) {
        PathElement[] result = new PathElement[pes.length];
        double top = -1, bottom = -1;
        for(int i=0, lg=pes.length; i<lg; i++) {
            PathElement pe = pes[i];
            if (pe instanceof MoveTo) {
                MoveTo mt = (MoveTo) pe;
                if (top == -1) {
                    top = mt.getY();
                    bottom = mt.getY();
                    continue;
                }
                if (top > mt.getY()) {
                    top = mt.getY();
                }
                if (bottom < mt.getY()) {
                    bottom = mt.getY();
                }
            } else if (pe instanceof LineTo) {
                LineTo mt = (LineTo) pe;
                if (top == -1) {
                    top = mt.getY();
                    bottom = mt.getY();
                    continue;
                }
                if (top > mt.getY()) {
                    top = mt.getY();
                }
                if (bottom < mt.getY()) {
                    bottom = mt.getY();
                }
            } else if (pe instanceof VLineTo) {
                VLineTo mt = (VLineTo) pe;
                if (top == -1) {
                    top = mt.getY();
                    bottom = mt.getY();
                    continue;
                }
                if (top > mt.getY()) {
                    top = mt.getY();
                }
                if (bottom < mt.getY()) {
                    bottom = mt.getY();
                }
            }
        }
        for(int i=0, lg=pes.length; i<lg; i++) {
            PathElement pe = pes[i];
            if (pe instanceof MoveTo) {
                MoveTo mt = (MoveTo) pe;
                MoveTo mt2 = new MoveTo(mt.getX(), mt.getY());
                if (mt2.getY() == top) {
                    mt2.setY(top - h);
                }
                if (mt2.getY() == bottom) {
                    mt2.setY(bottom + h);
                }
                result[i] = mt2;
            } else if (pe instanceof LineTo) {
                LineTo mt = (LineTo) pe;
                LineTo mt2 = new LineTo(mt.getX(), mt.getY());
                if (mt2.getY() == top) {
                    mt2.setY(top - h);
                }
                if (mt2.getY() == bottom) {
                    mt2.setY(bottom + h);
                }
                result[i] = mt2;
            } else if (pe instanceof VLineTo) {
                VLineTo mt = (VLineTo) pe;
                VLineTo mt2 = new VLineTo(mt.getY());
                if (mt2.getY() == top) {
                    mt2.setY(top - h);
                }
                if (mt2.getY() == bottom) {
                    mt2.setY(bottom + h);
                }
                result[i] = mt2;
            } else if (pe instanceof HLineTo) {
                HLineTo mt = (HLineTo) pe;
                result[i] = new VLineTo(mt.getX());
            } else if (pe instanceof ClosePath) {
                result[i] = new ClosePath();
            }
        }
        return result;
    }
}
